<?php

define('_ADODB_LAYER',1);
define('ADODB_DIR', dirname(__FILE__));
define('ADODB_FETCH_DEFAULT',0);
define('ADODB_FETCH_NUM',1);
define('ADODB_FETCH_ASSOC',2);
define('ADODB_FETCH_BOTH',3);
//$ADODB_FETCH_MODE = ADODB_FETCH_ASSOC;

/*public $password;
public $username;
public $host;
public $database;
*/
/*define( 'DB_USER', $username );   //wpisujesz swoje    
define( 'DB_PASS', $password );      //wpisujesz swoje
define( 'DB_HOST', $host );    //wpisujesz swoje
define( 'DB_DB', $database );      //wpisujesz swoje
*/
class NewADOConnection
{
	public $connectionId = false;
	public $record_set = false;
	public $dbtype = 'mysql';
	public $open;
	private $debug = false;
	public $debug_echo = true;
	public $debug_output;
	private $createdatabase = false;
	public $socket = false;
	public $port = false;
	public $clientFlags = 0;
	private $nameQuote = '`';
	private $sysDate = 'CURDATE()';
	private $sysTimeStamp = 'NOW()';
	public $number;

	function Connect($host1, $username1, $password1, $database1)
	{
		//$password = $password1; // pass tez bez ''
		//$username = $username1; // uzytkownik rowniez bez '' 
		//$host = $host1; //host bazy danych bez '' 
		//$database = $database1;  // nazwa bazy danych bez ''
		$this->connectionId = mysql_pconnect( $host1, $username1, $password1, 0);
		$result = mysql_select_db( $database1, $this->connectionId );
		return $result;
	}
	
		
	public function __construct()
	{
		global $_SESSION;
		$_SESSION['numq'] = 0;		
	}
	function Execute( $sql, $inputarr = false ){
		$this -> number += 1;
		$rs = $this->do_query($sql, -1, -1, $inputarr);
		//echo mysql_error();
		return $rs;
	}
	function cacheFlush(){
	}
	function cacheExecute( $sql, $inputarr = false ){
		$this -> number += 1;
		$rs = $this->do_query($sql, -1, -1, $inputarr);
		//echo mysql_error();
		return $rs;
	}
	function ExecuteU($sql){
		$this -> number += 1;
		mysql_unbuffered_query($sql);
	}
	function DBDate($d){
		if (empty($d) && $d !== 0) return 'null';
		if ($d === 'null' || strncmp($d,"'",1) === 0) return $d;
                $d = '\''.$d.'\'';
		return $d;
	}
	/*	function Ping(){
	mysql_ping();
	return true;
	}*/
	function SelectLimit( $sql, $nrows=-1, $offset=-1, $inputarr=false, $secs2cache=0 ){
		$rs = $this->do_query( $sql, $offset, $nrows, $inputarr);
		return $rs;
	}
	function outp($text, $newline = false){
		$this->debug_output = "<br>\n(" . $this->dbtype . "): ".htmlspecialchars($text)."<br>\n Error (" . $this->ErrorNo() .'): '. $this->ErrorMsg() . "<br>\n";
		if($this->debug_echo)
		echo $this->debug_output;
	}

/*	function Connect(){
		//$this->connectionId = mysql_pconnect( $this->host, $this->username, $this->password, 0);
		$this->connectionId = mysql_pconnect( $this->host, $this->username, $this->password, 0);
		$result = mysql_select_db( $this->database, $this->connectionId );
		return $result;
	}
	*/
	function ErrorMsg(){ return mysql_error($this->connectionId);}
	function ErrorNo(){ return mysql_errno($this->connectionId);}
	function Affected_Rows(){ return mysql_affected_rows($this->connectionId);}
	function Insert_ID(){ return mysql_insert_id($this->connectionId);}
	function qstr($string, $magic_quotes=false){
		if (!$magic_quotes) {
			return "'" . mysql_real_escape_string($string) . "'";
			$string = str_replace("'", "\\'" , str_replace('\\', '\\\\', str_replace("\0", "\\\0", $string)));
			return  "'" . $string . "'";
		}
		return "'" . str_replace('\\"', '"', $string) . "'";
	}
	function Concat(){
		$arr = func_get_args();
		$list = implode(', ', $arr);

		if (strlen($list) > 0) return "CONCAT($list)";
		else return '';
	}
	function IfNull( $field, $ifNull ){ return ' IFNULL('.$field.', '.$ifNull.') ';}
	function Close(){
		mysql_close( $this->connectionId );
		$this->connectionId = false;
	}
	
	function GetRow($sql,$inputarr=false)
	{
	global $ADODB_COUNTRECS;
		$crecs = $ADODB_COUNTRECS;
		$ADODB_COUNTRECS = false;
		
		$rs = $this->Execute($sql,$inputarr);
		
		$ADODB_COUNTRECS = $crecs;
		if ($rs) {
			if (!$rs->EOF) $arr = $rs->fields;
			else $arr = array();
			$rs->Close();
			return $arr;
		}
		
		$false = false;
		return $false;
	}
	
	function GetAll($sql, $inputarr = false){
		$data = $this->GetArray($sql, $inputarr);
		return $data;
	}
	function GetArray($sql, $inputarr = false){
		$data = false;
		$result = $this->Execute($sql, $inputarr);
		if ($result)
		{
			$data = $result->GetArray();
			$result->Close();
		}
		return $data;
	}
	function do_query( $sql, $offset, $nrows, $inputarr=false ){
		global $ADODB_FETCH_MODE, $_SESSION;
		$_SESSION['numq']++;
		$false = false;
		$limit = '';
		if ($offset != -1 || $nrows != -1){
			$offset = ($offset>=0) ? $offset . "," : '';
			$limit = ' LIMIT ' . $offset . ' ' . $nrows;
		}
		if ($inputarr && is_array($inputarr)) {
			$sqlarr = explode('?', $sql);
			if (!is_array(reset($inputarr))) $inputarr = array($inputarr);
			foreach($inputarr as $arr) {
				$sql = ''; $i = 0;
				foreach($arr as $v) {
					$sql .= $sqlarr[$i++];
					switch(gettype($v)){
						case 'string':
							$sql .= $this->qstr($v);
							break;
						case 'double':
							$sql .= str_replace(',', '.', $v);
							break;
						case 'boolean':
							$sql .= $v ? 1 : 0;
							break;
						default:
							if ($v === null)
							$sql .= 'NULL';
							else $sql .= $v;
					}
				}
				$sql .= $sqlarr[$i];
				if ($i+1 != sizeof($sqlarr)) return $false;
				$resultId = mysql_query( $sql . $limit, $this->connectionId );
				if($this->debug)$this->outp($sql . $limit);
				if ($resultId === false) return $false;
			}
		}else{
			$resultId = mysql_query( $sql . $limit, $this->connectionId );
			if($this->debug)$this->outp($sql . $limit);
		}

		if ($resultId === false) return $false;
		if ($resultId === true) {
			$rs = new ADORecordSet_empty();
			return $rs;
		}
		$recordset = new mysql_driver_ResultSet( $resultId, $this->connectionId );
		$recordset->_currentRow = 0;
		$recordset->fetchMode = MYSQL_ASSOC;
		$recordset->_numOfRows = mysql_num_rows( $resultId );
		if( $recordset->_numOfRows == 0)$recordset->EOF = true;
		$recordset->_numOfFields = mysql_num_fields( $resultId );
		$recordset->_fetch();
		return $recordset;
	}
}

class mysql_driver_ResultSet
{
	var $connectionId;
	var $fields;
	var $resultId;
	var $_currentRow = 0;
	var $_numOfRows = -1;
	var $_numOfFields = -1;
	var $fetchMode;
	var $EOF;

	function mysql_driver_ResultSet( $resultId, $connectionId ){
		$this->fields = array();
		$this->connectionId = $connectionId;
		$this->record = array();
		$this->resultId = $resultId;
		$this->EOF = false;
	}
	function Close(){
		mysql_free_result( $this->resultId );
		$this->fields = array();
		$this->resultId = false;
	}
	function fields( $field ){return $this->fields[$field];}
	function RecordCount(){return $this->_numOfRows;}
	function FieldCount(){return $this->_numOfFields;}
	function MoveNext(){
		if ($this->fields = mysql_fetch_array($this->resultId,$this->fetchMode)) {
			$this->_currentRow += 1;
			return true;
		}
		if (!$this->EOF) {
			$this->_currentRow += 1;
			$this->EOF = true;
		}
		return false;
	}
	function MoveFirst(){
		if ($this->_currentRow == 0) return true;
		return $this->Move(0);
	}
	function MoveLast(){
		if ($this->EOF) return false;
		return $this->Move($this->_numOfRows - 1);
	}
	function Move($rowNumber = 0){
		if ($rowNumber == $this->_currentRow) return true;
		$this->EOF = false;
		if ($this->_numOfRows > 0){
			if ($rowNumber >= $this->_numOfRows - 1){
				$rowNumber = $this->_numOfRows - 1;
				$this->EOF = true;
			}
		}
		if ($this->_seek($rowNumber)) {
			$this->_currentRow = $rowNumber;
			if ($this->_fetch()) return true;
			$this->fields = false;
		}
		$this->EOF = true;
		return false;
	}
	function _seek($row){
		if ($this->_numOfRows == 0) return false;
		return mysql_data_seek($this->resultId,$row);
	}
	function _fetch(){
		$this->fields = mysql_fetch_array($this->resultId,$this->fetchMode);
		return is_array($this->fields);
	}
	function EOF(){
		if( $this->_currentRow < $this->_numOfRows) return false;
		else{
			$this->EOF = true;
			return true;
		}
	}
	function GetArray($nRows = -1){
		$results = array();
		$cnt = 0;
		while (!$this->EOF && $nRows != $cnt) {
			$results[] = $this->fields;
			$this->MoveNext();
			$cnt++;
		}
		return $results;
	}
	function GetRows($nRows = -1){
		$arr = $this->GetArray($nRows);
		return $arr;
	}
	function GetAll($nRows = -1){
		$arr = $this->GetArray($nRows);
		return $arr;
	}
	function FetchField($fieldOffset = -1){
		if ($fieldOffset != -1) {
			$fieldObject = mysql_fetch_field($this->resultId, $fieldOffset);
			$fieldObject->max_length = mysql_field_len($this->resultId,$fieldOffset);
		}else{
			$fieldObject = mysql_fetch_field($this->resultId);
			$fieldObject->max_length = mysql_field_len($this->resultId);
		}
		return $fieldObject;
	}

}

class ADORecordSet_empty {
	var $fields = false;
	var $EOF = true;
	function MoveNext() {return;}
	function RecordCount() {return 0;}
	function FieldCount() {return 0;}
	function EOF(){return TRUE;}
	function Close(){return true;}
}
class ADOFieldObject {
	var $name = '';
	var $max_length=0;
	var $type="";
}

?>
